<?php
class Cookie
{
    public $data = [];
    public function __construct()
    {
        $data = $_COOKIE;
        foreach ($data as $key=>$value){
            $data[$key] = $this->parseValue($value);
        }
        $this->data = $data; //缓存了
    }


    /**
     * 设置存储cookie
     * @param string $name 键名
     * @param mixed $value 值
     * @param int $minutes 储存秒数
     * @param string $path 存储路径
     * @param null $domain 有效域名/子域名
     * @param bool $secure 是否通过安全的HTTPS连接传给客户端
     * @param bool $httpOnly 设置成 TRUE，仅可通过HTTP协议访问
     */
    public function make($name, $value, $minutes = 0, $path = "/", $domain = null, $secure = false, $httpOnly = true)
    {
        setcookie($name, $this->initValue($value), empty($minutes) ? 0 : (time() + $minutes), $path, $domain, $secure, $httpOnly);
        $this->data[$name] = $value;
    }

    /**
     * 删除cookie
     * @param $name
     */
    public function del($name)
    {
        setcookie($name, "", -1);
        unset($this->data[$name]);
    }

    /**
     * 获取cookie
     * @param $name
     * @param null $default
     * @return mixed|null
     */
    public function fetch($name = null, $default = null)
    {
        if (is_null($name)) {
            return $this->get();
        }
        if ($this->has($name)) {
            return $this->parseValue($this->data[$name]);
        }
        return $default;
    }

    /**
     * 验证cookie是否存在
     * @param $name
     * @return bool
     */
    public function has($name)
    {
        return isset($this->data[$name]);
    }

    /**
     * 初始化值
     * @param $value
     * @return array|int|string
     */
    protected function initValue($value)
    {
        if (is_object($value)) {
            // error("Cookie 不支持对象储存");
            throw new \Exception("Cookie 不支持对象储存",1);
        }
        $gather = array("_gather" => false, "value" => $value);
        return json_encode($gather,JSON_UNESCAPED_UNICODE);
    }

    /**
     * 解析value值
     * @param $value
     * @return array|mixed|null|object
     */
    protected function parseValue($value)
    {
        $data = $value;
        if(!is_array($value)) {
            try{
                // dd($value);
                $data = json_decode($value,true);
            }catch (\Exception $e){
                $data = null;
            }
        }
        
        if(!is_null($data) && is_array($data)) {
            if (array_key_exists("_gather",$data) && array_key_exists("value",$data)) {
                $value = $data["value"];
            }else {
                $value = $data;
            }
        }

        return $value;
    }
    /**
     * 数组 转 对象
     * @param array $arr 数组
     * @return object
     */
    public function arrayToObject($arr) {
        if (gettype($arr) != 'array') {
            return;
        }
        foreach ($arr as $k => $v) {
            if (gettype($v) == 'array' || getType($v) == 'object') {
                $arr[$k] = (object)$this->arrayToObject($v);
            }
        }

        return (object)$arr;
    }

    /**
     * 对象 转 数组
     * @param object $obj 对象
     * @return array
     */
    public function objectToArray($obj) {
        $obj = (array)$obj;
        foreach ($obj as $k => $v) {
            if (gettype($v) == 'resource') {
                return;
            }
            if (gettype($v) == 'object' || gettype($v) == 'array') {
                $obj[$k] = (array)$this->objectToArray($v);
            }
        }
        return $obj;
    }

//========================================================================================

    /**
     * 获取值
     * @param $name
     * @return bool
     */
    public function get($name = null, $default = null){
        if (is_null($name)) {
            return $this->data;
        }
        if ($this->has($name)) {
            return $this->parseValue($this->data[$name]);
        }
        return $default;
    }

}


